library(tidyverse)

dt <- tibble(
  age=c(24,22,23,25,22),
  personality=c('g','b','g','b','g'),
  income=c(2000,5800,4200,1500,6000)
)

dt$age
dt['age']
dt[1]
dt[[1]]
dt[2,3]
dt[,2]
dt[4,]
dt[1:3,c('age','income')]

str(diamonds)
structure(diamonds)
filter(diamonds, cut == 'Ideal' & carat > 3)
filter(diamonds, carat <= .2| carat >= 3)

slice(diamonds,1:10)
slice(diamonds,(n()-9):n())

select(diamonds,cut,price)
select(diamonds,-x,-y,-z)

mutate(diamonds,
       price_per_carat=price/carat,
       volume=x*y*z,
       price_per_volume=price/volume)

arrange(diamonds,carat,price)
arrange(diamonds,carat,desc(price))

summarize(diamonds,avg_price=mean(price))

diamonds_by_cut <- group_by(diamonds,cut)
slice(diamonds_by_cut,1)
summarize(diamonds_by_cut,
          count=n(),
          avg_price=mean(price)
)

by_cut <- group_by(diamonds,cut)
count_cuts <- mutate(by_cut,N=n())
view(count_cuts)

count_cuts_1 <- diamonds %>% group_by(cut) %>% mutate(N=n())
